package base;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import main.ImageLibrary;
import main.Telecommando;

public class Button {
	private String name = "";
	private String img;
	private int size = 8;
	private boolean pressed = false;
	private boolean showName = true;
	private boolean showOverlay = true;
	private Rectangle rect = null;
	private Rectangle initRect = null;
	private String overlayUp = "Images/ButtonOverlayUp.png";
	private String overlayDown = "Images/ButtonOverlayDown.png";
	private Color color = Color.white;
	public Button( String name , Point pos , String imgLocation )
	{
		this.name = name;
		img = imgLocation;
		if( !ImageLibrary.contains( imgLocation ) )
			ImageLibrary.add( img , imgLocation );
		rect = new Rectangle( pos.x , pos.y , ImageLibrary.get( img ).getWidth( ) , ImageLibrary.get( img ).getHeight( ) );
		initRect = new Rectangle( rect );
		loadOverlays( );
	}
	public Button( String name , Point pos , String imgLocation , boolean showName , boolean showOverlay)
	{
		this.name = name;
		this.showName = showName;
		this.showOverlay = showOverlay;
		img = imgLocation;
		if( !ImageLibrary.contains( imgLocation ) )
			ImageLibrary.add( img , imgLocation );
		rect = new Rectangle( pos.x , pos.y , ImageLibrary.get( img ).getWidth( ) , ImageLibrary.get( img ).getHeight( ) );
		initRect = new Rectangle( rect );
		loadOverlays( );
	}

	public Button( String name , Point pos , String imgLocation , boolean showName , boolean showOverlay , String overlayUp , String overlayDown )
	{
		this.name = name;
		this.showName = showName;
		this.showOverlay = showOverlay;
		this.overlayUp = overlayUp;
		this.overlayDown = overlayDown;
		img = imgLocation;
		if( !ImageLibrary.contains( imgLocation ) )
			ImageLibrary.add( img , imgLocation );
		rect = new Rectangle( pos.x , pos.y , ImageLibrary.get( img ).getWidth( ) , ImageLibrary.get( img ).getHeight( ) );
		initRect = new Rectangle( rect );
		loadOverlays( );
	}

	public String getImage( )
	{
		return img;
	}

	public void setImage( String img )
	{
		if( !ImageLibrary.contains( img ) )
			ImageLibrary.add( img , img );
		this.img = img;
	}

	public void setColor( Color color )
	{
		this.color = color;
	}

	public void setTextSize( int size )
	{
		this.size = size;
	}

	public boolean isPressed( )
	{
		return pressed;
	}

	public String getName( )
	{
		return name;
	}

	private void loadOverlays( )
	{
		if( !ImageLibrary.contains( img ) )
			ImageLibrary.add( img , img );
		makeOverlay( overlayUp , overlayUp );
		makeOverlay( overlayDown , overlayDown );
		int size = 64;
		if( rect.width == rect.height && rect.width == size )
		{
			if( makeOverlay( overlayUp + size , overlayUp ) )
				ImageLibrary.resize( overlayUp + size , 0.5 , 0.5 );
			if( makeOverlay( overlayDown + size , overlayDown ) )
				ImageLibrary.resize( overlayDown + size , 0.5 , 0.5 );
		}
		size = 96;
		if( rect.width == rect.height && rect.width == size )
		{
			if( makeOverlay( overlayUp + size , overlayUp ) )
				ImageLibrary.resize( overlayUp + size , 0.75 , 0.75 );
			if( makeOverlay( overlayDown + size , overlayDown ) )
				ImageLibrary.resize( overlayDown + size , 0.75 , 0.75 );
		}
		size = 32;
		if( rect.width == rect.height && rect.width == size )
		{
			if( makeOverlay( overlayUp + size , overlayUp ) )
				ImageLibrary.resize( overlayUp + size , 0.25 , 0.25 );
			if( makeOverlay( overlayDown + size , overlayDown ) )
				ImageLibrary.resize( overlayDown + size , 0.25 , 0.25 );
		}
	}

	private boolean makeOverlay( String name , String img )
	{
		if( !ImageLibrary.contains( name ) )
		{
			ImageLibrary.add( name , img );
			return true;
		}
		return false;
	}

	public boolean press( int x , int y ) {
		if( rect.contains( x , y ) )
		{
			pressed = true;
			return true;
		}
		return false;
	}

	public void release( )
	{
		pressed = false;
		//rotate = 0;
	}

	public void setX( int val ){ rect.x = val; }
	public void setY( int val ){ rect.y = val; }
	public int getX( ){return rect.x;}
	public int getY( ){return rect.y;}
	public int getWidth( ){return rect.width;}
	public int getHeight( ){return rect.height;}
	public Rectangle getRect( ){return rect;}
	public Rectangle getInitRect( ){return initRect;}

	private static final Font font = new Font( "temp" , Font.PLAIN , 8 );
	public void draw( Graphics2D g )
	{
		g.drawImage( ImageLibrary.get( img ) , rect.x , rect.y , Telecommando.getInstance( ) );
		if( showOverlay )
		{
			BufferedImage img = null;
			if( rect.width == rect.height )
			{
				int size = rect.width;
				if( size == 32 || size == 64 || size == 96 )
				{
					if( pressed )
						img = ImageLibrary.get( overlayDown + size );
					else
						img = ImageLibrary.get( overlayUp + size );
				}
				else if( size == 128 )
				{
					if( pressed )
						img = ImageLibrary.get( overlayDown );
					else
						img = ImageLibrary.get( overlayUp );
				}
			}
			if( img == null )
			{
				if( pressed )
					img = ImageLibrary.get( overlayDown );
				else
					img = ImageLibrary.get( overlayUp );
				double scaleX = rect.getWidth( )/128.0;
				double scaleY = rect.getHeight( )/128.0;
				img = ImageLibrary.getScaledBufferedImage( img , scaleX , scaleY );
			}
			g.drawImage( img , rect.x , rect.y , Telecommando.getInstance( ) );
			int idBs = name.indexOf( '[' );
			if( idBs != -1 )
			{
				String strID = name.substring( idBs+1 , name.indexOf( "]" ) );
				int id = -1;
				try{ id = Integer.parseInt( strID ); } catch( Exception e ){ }
				if( id != -1 )
					g.drawImage( ImageLibrary.get( "r[" + id + "]" ) , rect.x + rect.width - 16 , rect.y , Telecommando.getInstance( ) );
			}
		}
		if( showName )
		{
			g.setColor( color );
			if( size == 8 )
				g.setFont( font );
			else
				g.setFont( new Font( "temp2" , Font.PLAIN , size ) );
			g.drawString( name , rect.x , rect.y + rect.height + 8 );
			g.setFont( Telecommando.normFont );
		}
	}
}
